﻿// <copyright file="CacheValue.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   The cache tuple is designed to help distinguish between cache key values of the same type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Caching
{
    using System;
    using Common;

    public sealed class CacheTuple<T1, T2> : BaseModel, IEquatable<CacheTuple<T1, T2>>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CacheTuple&lt;T1, T2&gt;"/> class.
        /// </summary>
        public CacheTuple()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CacheTuple&lt;T1, T2&gt;"/> class.
        /// </summary>
        /// <param name="val1">
        /// Tuple value 1
        /// </param>
        /// <param name="val2">
        /// Tuple value 2
        /// </param>
        public CacheTuple(T1 val1, T2 val2)
        {
            this.Val1 = val1;
            this.Val2 = val2;
        }

        #endregion

        #region Properties

        public T1 Val1 { get; set; }

        public T2 Val2 { get; set; }

        #endregion

        #region Public Methods

        public static bool operator ==(CacheTuple<T1, T2> ct1, CacheTuple<T1, T2> ct2)
        {
            return Equals(ct1, ct2);
        }

        public static bool operator !=(CacheTuple<T1, T2> ct1, CacheTuple<T1, T2> ct2)
        {
            return !Equals(ct1, ct2);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }

            return obj is CacheTuple<T1, T2> && this.Equals((CacheTuple<T1, T2>)obj);
        }

        public bool Equals(CacheTuple<T1, T2> tuple)
        {
            if (ReferenceEquals(null, tuple))
            {
                return false;
            }

            if (!Equals(this.Val1, tuple.Val1))
            {
                return false;
            }

            if (!Equals(this.Val2, tuple.Val2))
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// The get hash code.
        /// </summary>
        /// <returns>
        /// Returns the combined hash code of the properties of the class.
        /// </returns>
        public override int GetHashCode()
        {
            return this.CombineHashCodes(this.Val1, this.Val2);
        }

        #endregion
    }
}